package com.mzj.saas.mservice.property.service;

import com.mzj.saas.commons.DateUtils;
import com.mzj.saas.commons.ResultBean;
import com.mzj.saas.commons.vo.redis.TokenRedisVo;
import com.mzj.saas.mservice.property.common.ExceptionTypeEnum;
import com.mzj.saas.mservice.property.entity.CommunityHouseException;
import com.mzj.saas.mservice.property.repository.CommunityHouseExceptionRepository;
import com.mzj.saas.mservice.property.vo.CommunityHouseExceptionVO;
import com.mzj.saas.redis.RedisService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;


@Service
public class CommunityHouseExceptionService {
    private static final Logger log = LoggerFactory.getLogger(CommunityHouseExceptionService.class);

    @Autowired
    private RedisService redisService;
    @Autowired
    private CommunityHouseExceptionRepository communityHouseExceptionRepository;
    @Autowired
    @Qualifier("stringRedisTemplate")
    private StringRedisTemplate redisTemplate;

    /**
     * 查看房屋状态记录
     * @param accessToken
     * @param communityHouseId
     *
     * 查询当前房产状态记录并排序
     *
     */
    public ResultBean<List<CommunityHouseExceptionVO>> list(String accessToken, String communityHouseId) {
        try {
            //校验登录信息
            TokenRedisVo token = redisService.findTokenVo(accessToken);
            if (token == null) return ResultBean.failedResultOfToken();

            List<CommunityHouseException> exceptions = communityHouseExceptionRepository.findByCommunityHouseId(communityHouseId);

            List<CommunityHouseExceptionVO> voList = new ArrayList<>();

            for (CommunityHouseException exception : exceptions) {
                CommunityHouseExceptionVO vo = convertToVO(exception);
                voList.add(vo);
            }
            //按时间排序
            Collections.sort(voList, (o1, o2) -> o2.getOperatorTime().compareTo(o1.getOperatorTime()));

            return ResultBean.successfulResult(voList);

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultBean.failedResultOfException();
        }
    }

    private CommunityHouseExceptionVO convertToVO(CommunityHouseException communityHouseException) {
        CommunityHouseExceptionVO vo = new CommunityHouseExceptionVO();
        vo.setId(communityHouseException.getId());
        vo.setCommunityHouseId(communityHouseException.getCommunityHouseId());
        vo.setType(communityHouseException.getType());
        vo.setTypeName(ExceptionTypeEnum.getTypeName(vo.getType()));
        vo.setDescription(communityHouseException.getDescription());
        vo.setOperatorId(communityHouseException.getOperatorId());
        vo.setOperatorTime(communityHouseException.getOperatorTime());
        return vo;
    }

    /**
     * 房屋状态登记
     * @param accessToken
     * @param vo
     * @return
     * 设置当前房屋状态  每日登记一次
     */
    public ResultBean<CommunityHouseExceptionVO> save(String accessToken, CommunityHouseExceptionVO vo) {
        try {
            TokenRedisVo token = redisService.findTokenVo(accessToken);
            if (token == null) return ResultBean.failedResultOfToken();

            String communityHouseId = vo.getCommunityHouseId();
            String redis = redisTemplate.opsForValue().get("houseException:" + communityHouseId);
            if (StringUtils.isNotBlank(redis)) {
                return ResultBean.failedResultWithMsg("房屋状态已设置，请明天再试");
            }

            CommunityHouseException che = new CommunityHouseException();
            che.setCommunityHouseId(vo.getCommunityHouseId());
            che.setType(vo.getType());
            che.setDescription(vo.getDescription());
            che.setOperatorId(token.getId());
            che.setOperatorTime(new Date());

            communityHouseExceptionRepository.save(che);

            //设置每日只能更改一次 凌晨0.00刷新
            redisTemplate.opsForValue().set("houseException:" + communityHouseId, "1", DateUtils.getTimeMillis(), TimeUnit.SECONDS);

            return ResultBean.successfulResult(convertToVO(che));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultBean.failedResultOfException();
        }
    }


}
